package service.impl;

import entity.Competitions;
import mapper.CompetitionsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.CompetitionsService;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class CompetitionsImpl implements CompetitionsService {

    @Autowired
    private CompetitionsMapper mapper;

    @Override
    public Competitions getById(Integer id) {
        return mapper.selectById(id);
    }

    @Override
    public List<Competitions> getAll() {
        return mapper.selectAll();
    }

    @Override
    public List<Competitions> getByStudentId(Integer sid) {
        return mapper.selectByStudentId(sid);
    }

    @Override
    public int create(Competitions c) {
        LocalDateTime now = LocalDateTime.now();
        c.setCreatedAt(now);
        c.setUpdatedAt(now);

        // 自动计算得分
        c.setScore(calculateScore(c));

        return mapper.insert(c);
    }

    @Override
    public int update(Competitions c) {
        c.setUpdatedAt(LocalDateTime.now());

        // 自动计算得分
        c.setScore(calculateScore(c));

        return mapper.update(c);
    }

    @Override
    public int delete(Integer id) {
        return mapper.delete(id);
    }

    /**
     * 根据竞赛级别、类别和获奖等级计算得分
     */
    private BigDecimal calculateScore(Competitions competition) {
        BigDecimal baseScore = BigDecimal.ZERO;

        // 根据竞赛级别确定基础分值
        switch (competition.getLevel()) {
            case "国家级":
                baseScore = new BigDecimal("100");
                break;
            case "省级":
                baseScore = new BigDecimal("80");
                break;
            case "市级":
                baseScore = new BigDecimal("60");
                break;
            case "校级":
                baseScore = new BigDecimal("40");
                break;
            case "CCF":
                baseScore = new BigDecimal("90");
                break;
            default:
                baseScore = BigDecimal.ZERO;
        }

        // 根据竞赛类别调整分值
        switch (competition.getCategory()) {
            case "A1":
                baseScore = baseScore.multiply(new BigDecimal("1.0"));
                break;
            case "A2":
                baseScore = baseScore.multiply(new BigDecimal("0.8"));
                break;
            case "A3":
                baseScore = baseScore.multiply(new BigDecimal("0.6"));
                break;
        }

        // 根据获奖等级调整分值
        switch (competition.getPrize()) {
            case "一等奖":
                baseScore = baseScore.multiply(new BigDecimal("1.0"));
                break;
            case "二等奖":
                baseScore = baseScore.multiply(new BigDecimal("0.7"));
                break;
            case "三等奖":
                baseScore = baseScore.multiply(new BigDecimal("0.5"));
                break;
        }

        // 团队赛额外加分
        if (competition.getIsTeam()) {
            baseScore = baseScore.multiply(new BigDecimal("1.1"));
        }

        return baseScore.setScale(2, BigDecimal.ROUND_HALF_UP);
    }
}
